<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <title>羊了个羊</title>
    <style>
        * {
            box-sizing: border-box;
        }
        
        body {
            margin: 0;
        }
        
        .main {
            position: relative;
        }
        
        .item {
            position: absolute;
            background: no-repeat center center #ffffff;
            border: 1px solid #ddd;
            background-size: 100%;
            color: transparent;
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 0;
            transition: left 0.3s, top 0.3s, transform 0.1s;
        }
        
        .item:after {
            content: "";
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            transition: background-color 0.2s;
        }
        
        .disabled:after {
            background-color: rgba(0, 0, 0, 0.7);
        }
        
        .move-list {
            border: 1px solid #ddd;
            background-color: #ddd;
            margin: 0 auto;
        }
    </style>
</head>

<body>
    <div class="main"></div>
    <div class="move-list"></div>
</body>
<script>
    1
    // 基础数据
    const simpleData = [{
        name: "刷子",
        color: "#ff1100",
        bg: "https://img.clinicmed.net/uploadimg/image/20221116/20221116110857_67329.jpg",
    }, {
        name: "奶瓶",
        color: "#ff8800",
        bg: "https://img.clinicmed.net/uploadimg/image/20221116/20221116111114_92056.jpg",
    }, {
        name: "玉米",
        color: "green",
        bg: "https://img.clinicmed.net/uploadimg/image/20221116/20221116111348_83578.jpg",
    }, {
        name: "粪叉",
        color: "blue",
        bg: "https://img.clinicmed.net/uploadimg/image/20221116/20221116110838_97880.jpg",
    }, {
        name: "草",
        color: "#779922",
        bg: "https://img.clinicmed.net/uploadimg/image/20221116/20221116111500_79089.jpg",
    }, {
        name: "白菜",
        color: "#335577",
        bg: "https://img.clinicmed.net/uploadimg/image/20221116/20221116111105_26982.jpg",
    }, {
        name: "草垛",
        color: "#335577",
        bg: "https://img.clinicmed.net/uploadimg/image/20221116/20221116110823_14228.jpg",
    }, {
        name: "小木墩",
        color: "#335577",
        bg: "https://img.clinicmed.net/uploadimg/image/20221116/20221116111019_19911.jpg",
    }, {
        name: "胡萝卜",
        color: "#335577",
        bg: "https://img.clinicmed.net/uploadimg/image/20221116/20221116111509_62273.jpg",
    }, {
        name: "黄铃铛",
        color: "#335577",
        bg: "https://img.clinicmed.net/uploadimg/image/20221116/20221116111517_86581.jpg",
    }, {
        name: "白棉花",
        color: "#335577",
        bg: "https://img.clinicmed.net/uploadimg/image/20221116/20221116111527_63174.jpg",
    }, {
        name: "小火堆",
        color: "#335577",
        bg: "https://img.clinicmed.net/uploadimg/image/20221116/20221116111122_73680.jpg",
    }, {
        name: "红水桶",
        color: "#335577",
        bg: "https://img.clinicmed.net/uploadimg/image/20221116/20221116111356_67362.jpg",
    }, {
        name: "蓝手套",
        color: "#335577",
        bg: "https://img.clinicmed.net/uploadimg/image/20221116/20221116111404_35203.jpg",
    }, ];
    // 卡片大小
    const size = 40;
    // 矩阵行
    const rows = 10;
    // 矩阵列
    const cols = 10;
    // 每组为3个
    const oneGoupCount = 3;
    // 每个卡片有x组
    const group = 15;
    // 总共6层，即6个表格
    const layerCount = 60;
    // 表格html
    const cellHtml = [];
    // 将生成所有的卡片
    const renderData = Array.from(new Array(oneGoupCount * group))
        .map(v => {
            return simpleData.map(v => ({
                ...v
            }));
        })
        .flat()
        .sort(v => Math.random() - 0.5);
    // 第一步：绘制出表格矩阵
    for (let ly = layerCount - 1; ly >= 0; ly--) {
        for (let i = 0; i < rows; i++) {
            for (let j = 0; j < cols; j++) {
                let pyStep = (ly + 1) % 2 === 0 ? size / 2 : 0;
                let item = Math.random() > 0.7 && renderData.pop();

                item &&
                    cellHtml.push(`<div class="item" onclick="move(this)" id="m${ly}-${i}-${j}"
                    style="width:${size}px;height:${size}px;left:${size * j + pyStep
                        }px;top:${size * i + pyStep}px;background-image:url(${item.bg || ""
                        })">${item.name || ""}</div>`);
            }
        }
    }
    const main = document.querySelector(".main");
    const moveList = document.querySelector(".move-list");
    main.innerHTML = cellHtml.reverse().join("");
    main.style.height = `${size * rows + size * 2}px`;
    main.style.width = `${size * cols}px`;
    moveList.style.height = `${size}px`;
    moveList.style.width = `${size * 6}px`;

    //可设置为无解版

    // 第二步：计算被遮住的底牌需标暗色
    const checkDisabled = items => {
        (items || main.querySelectorAll(".item")).forEach((v, i) => {
            const arr = v.id
                .substring(1)
                .split("-")
                .map(v => Number(v));
            const isPy = (arr[0] + 1) % 2 === 0;
            for (let i = arr[0] + 1; i <= layerCount - 1; i++) {
                const isPyB = (i + 1) % 2 === 0;
                if (isPy === isPyB) {
                    const el = main.querySelector(`#m${i}-${arr[1]}-${arr[2]}`);
                    if (el) {
                        v.classList.add("disabled");
                        break;
                    }
                } else if (isPy && !isPyB) {
                    if (![
                            `${i}-${arr[1]}-${arr[2]}`,
                            `${i}-${arr[1]}-${arr[2] + 1}`,
                            `${i}-${arr[1] + 1}-${arr[2]}`,
                            `${i}-${arr[1] + 1}-${arr[2] + 1}`,
                        ].every(k => {
                            return !main.querySelector("#m" + k);
                        })) {
                        v.classList.add("disabled");
                        break;
                    } else {
                        v.classList.remove("disabled");
                    }
                } else if (!isPy && isPyB) {
                    if (![
                            `${i}-${arr[1]}-${arr[2]}`,
                            `${i}-${arr[1]}-${arr[2] - 1}`,
                            `${i}-${arr[1] - 1}-${arr[2]}`,
                            `${i}-${arr[1] - 1}-${arr[2] - 1}`,
                        ].every(k => {
                            return !main.querySelector("#m" + k);
                        })) {
                        v.classList.add("disabled");
                        break;
                    } else {
                        v.classList.remove("disabled");
                    }
                }
            }
        });
    };
    // 第三步：点击卡片进行消除计算
    let canMove = true;
    // 点击棋子，动画移动
    const move = me => {
        let left = moveList.offsetLeft;
        let top = moveList.offsetTop;
        if (!canMove || me.className.indexOf("disabled") >= 0) {
            return;
        }
        canMove = false;
        if (moveList.children.length > 0) {
            let el = moveList.children[moveList.children.length - 1];
            left = el.offsetLeft + size;
            top = el.offsetTop;
        }
        me.style.top = `${top}px`;
        me.style.left = `${left}px`;
        me.transitionNamesCount = 0;
        me.ontransitionend = e => {
            me.transitionNamesCount++;
            if (me.transitionNamesCount === 2) {
                moveEnd(me);
                canMove = true;
            }
        };
    };
    // 动画结束的相关计算
    const moveEnd = me => {
        me.ontransitionend = null;
        me.setAttribute("onclick", "");
        moveList.appendChild(me);

        const findResult = [...moveList.children].filter(
            v => v.innerHTML === me.innerHTML
        );
        if (findResult.length === 3) {
            findResult.forEach(v => {
                v.ontransitionend = e => {
                    moveList.removeChild(v);
                    [...moveList.children].forEach((v, i) => {
                        v.style.left = `${i * size + moveList.offsetLeft}px`;
                    });
                };
                setTimeout(() => (v.style.transform = "scale(0)"));
            });
        }
        setTimeout(() => {
            if (moveList.children.length === 6) {
                alert("池子已满，游戏结束");
                return location.reload();
            } else if (main.children.length === 0) {
                alert("恭喜通关");
                return location.reload();
            }
        }, 1000);
        checkDisabled();
    };
    checkDisabled();
</script>

</html>